package org.infodavid.common.model.impl;

import java.io.Serializable;

import org.infodavid.common.model.impl.AbstractImprovedDataObjectImpl;
import org.infodavid.common.persistence.ICommented;
import org.infodavid.common.persistence.INamed;

/**
 * The Class AbstractNamedCommented.
 * @param <K> the key type
 */
public abstract class AbstractNamedCommented<K extends Serializable> extends
    AbstractImprovedDataObjectImpl<K> implements INamed, ICommented {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 2447668125917029925L;

	/** The comment. */
	private String comment;

	/** The name. */
	private String name;

	/**
	 * constructor.
	 * @throws Exception the exception
	 */
	protected AbstractNamedCommented() throws Exception {
		super();
	}

	/**
	 * constructor.
	 * @param keyArg the key
	 * @throws Exception the exception
	 */
	protected AbstractNamedCommented(final K keyArg) throws Exception {
		this();

		setKey(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof INamed)) {
			return false;
		}

		final INamed other = (INamed)obj;

		if (getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		}
		else if (!getName().equals(other.getName())) {
			return false;
		}

		return true;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#getComment()
	 */
	public String getComment() {
		return comment;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#getName()
	 */
	public String getName() {
		return name;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + (name == null ? 0 : name.hashCode());

		return result;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.ICommented#setComment(java.lang.String)
	 */
	public void setComment(final String value) {
		if (!equals(comment, value)) {
			final Object old = comment;
			comment = value;
			markModified(COMMENT, old, comment);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.model.INamed#setName(java.lang.String)
	 */
	public void setName(final String value) {
		if (!equals(name, value)) {
			final Object old = name;
			name = value;
			markModified(NAME, old, name);
		}
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#toString(java.lang.StringBuffer)
	 */
	@Override
	protected StringBuffer toString(final StringBuffer builder) {
		super.toString(builder);

		builder.append("[comment=");
		builder.append(comment);
		builder.append(", name=");
		builder.append(name);
		builder.append("]");

		return builder;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.impl.DataObjectImpl#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		comment = null;
		name = null;
	}
}
